import axios from 'axios'
import { MessageBox, Message } from 'element-ui'
import store from '@/store'
// import authentication from '@/utils/authentication'
// import authService from '@/utils/authservice'
import { getToken, removeToken } from './auth'
import router from '@/router'

// create an axios instance
const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
  withCredentials: true, // send cookies when cross-domain requests
  timeout: 500000000 // request timeout
})

service.interceptors.request.use(
  config => {
    // please modify it according to the actual situation
    // 方式一
    const token = getToken()
    // if (!token) {
    //   if (!config.url.includes('/eaccount/sgmwLogin') && !config.url.includes('/eaccount/Login')) {
    //     router.push('/login')
    //   }
    // }
    // config.headers['Authorization'] = `Bearer ${token}`
    // config.headers['Authorization'] = token
    // authService.acquireToken().then(token => {
    // // config.headers['Authorization'] = `Bearer ${token}`
    //      access_token = token
    // // })
    // // config.headers['Authorization'] = 'Bearer ' + token
    // })
    if (token) {
      config.headers['Authorization'] = `Bearer ${token}`
    }

    return config
  },
  error => {
    // do something with request error
    console.log(error) // for debug
    return Promise.reject(error)
  }
)

// // request interceptor
// service.interceptors.request.use(
//   config => {
//     // do something before request is sent
//     // authentication.acquireToken()
//     if (store.getters.token) {
//       // let each request carry token
//       // ['X-Token'] is a custom headers key
//       // please modify it according to the actual situation
//       // config.headers['X-Token'] = getToken()
//       // Set default request headers for every request
//       // config.headers.set('Content-Type', 'application/json');
//       // config.headers.set('Ocp-Apim-Subscription-Key', 'api key');
//       config.headers['Authorization'] = 'Bearer ' + getToken()
//     }
//     return config
//   },
//   error => {
//     // do something with request error
//     console.log(error) // for debug
//     return Promise.reject(error)
//   }
// )

// response interceptor
service.interceptors.response.use(
  /**
   * If you want to get http information such as headers or status
   * Please return  response => response
  */

  /**
   * Determine the request status by custom code
   * Here is just an example
   * You can also judge the status by HTTP Status Code
   */
  response => {
    const res = response.data

    if (res.code == 401) {
      removeToken()
      return res
    }
    
    return res
    // if the custom code is not 20000, it is judged as an error.
    // if (res.code !== 200) {
     
    //   Message({
    //     message: res.message || 'error' + res.code,
    //     type: 'error',
    //     duration: 5 * 1000
    //   })
    //   return res
    // } else {
    //   return res
    // }
  },
  error => {
    if (error.response.status === 401) {
      removeToken()
      return
    }
    if (error.response.status === 400) {
      debugger
      const { data } = error.response

      Message({
        message: data.error,
        type: 'error',
        duration: 5 * 1000
      })
    } else {
      console.log('err' + error) // for debug
      Message({
        message: error.message,
        type: 'error',
        duration: 5 * 1000
      })
    }
    return Promise.reject(error)
  }
)

export default service
